package com.ns.scs.collections.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.sentinel.LimitType;
import com.ns.core.sentinel.RateLimiter;
import com.ns.core.utils.StpUtil;
import com.ns.core.utils.sg.ResultOk;
import com.ns.scs.collections.bo.CollectionsBO;
import com.ns.scs.collections.bo.CollectionsPageBO;
import com.ns.scs.collections.dto.CollectionsPageDto;
import com.ns.scs.collections.entry.CollectionsEntry;
import com.ns.scs.collections.service.ICollectionsService;
import com.ns.scs.collections.vo.CollectionsVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 收藏模块相关操作
 *
 * @author lw
 * @since 2024-11-15
 */
@RestController
@RequestMapping("/collections")
@RequiredArgsConstructor
@CrossOrigin
public class CollectionsController {


    private final ICollectionsService service;

    /**
     * 收藏
     *
     * @param type         资源来源
     * @param resourceId   资源id
     * @param resourceName 资源名称
     */
    @GetMapping("/{type}")
    @ReqLog(module = "收藏模块", name = "收藏")
    ResultOk collect(@PathVariable String type, Long resourceId, String resourceName) {
        // 登录用户id
        Long uid = StpUtil.getLoginUserId();
        List<CollectionsVO> collections = service.getByResourceIdAndUidAndType(resourceId, uid, type);
        boolean isSave = false;
        if (CollectionUtils.isEmpty(collections)) {
            CollectionsEntry entry = new CollectionsEntry();
            entry.setType(type);
            entry.setCollector(uid);
            entry.setCollector(StpUtil.getLoginUserId());
            entry.setResourceId(resourceId);
            entry.setResourceName(resourceName);
            isSave = service.save(entry);
        } else {
            List<Long> ids = collections.stream().map(CollectionsVO::getId).collect(Collectors.toList());
            service.removeBatchByIds(ids);
        }
        return ResultOk.getSuccess(isSave ? "已收藏" : "取消收藏", isSave);
    }


    /**
     * 增
     *
     * @param bo 参数
     */
    @GetMapping("add")
    @ReqLog(module = "收藏模块", name = "增")
    ResultOk add(@RequestBody CollectionsBO bo) {
        CollectionsEntry entry = new CollectionsEntry();
        BeanUtils.copyProperties(bo, entry);
        entry.setCollector(StpUtil.getLoginUserId());
        return ResultOk.getByBoolean(service.save(entry));
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "收藏模块", name = "逻辑删")
    ResultOk delete(Long id) {
        return ResultOk.getByBoolean(service.removeById(id));
    }

    /**
     * 删 - 根据id列表
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "收藏模块", name = "批量删")
    public ResultOk deleteByIds(@RequestBody IdsBO bo) {
        return ResultOk.getByBoolean(service.removeBatchByIds(bo.getIds()));
    }

    /**
     * 查询详情
     *
     * @param id 参数
     */
    @GetMapping("getById")
    @ReqLog(module = "收藏模块", name = "详情")
    public ResultOk get(Long id) {
        return ResultOk.getSuccessData(service.getById(id));
    }

    /**
     * 查-集合
     *
     * @param bo 参数
     */
    @PostMapping("getList")
    @ReqLog(module = "收藏模块", name = "查-集合")
    @RateLimiter(time = 5, count = 15, limitType = LimitType.IP)
    public ResultOk getList(@RequestBody CollectionsPageBO bo) {
        CollectionsPageDto dto = new CollectionsPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<CollectionsVO> page = service.getList(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }
}
